// pages/adressList/adressList.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: '',
    list: [

    ],
    setAdId: ''
  },

  ok: (e) => {
    wx.navigateTo({
      url: `./../newaddress/newaddress?id=${e.currentTarget.dataset.id}&userId=${e.currentTarget.dataset.userid}`
    })
  },
  toAddress: () => {
    wx.navigateTo({
      url: '../newaddress/newaddress'
    })
  },
  delete(e) {
    let that = this
    wx.showLoading({
      title: '处理中',
      mask: true
    })
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=drop_consignee',
      data: {
        address_id: e.currentTarget.dataset.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.getAddress()
        wx.hideLoading()

      }
    })
  },
  getAddress() {
    let that = this
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=address_list', //地址列表
      data: {
        openid: app.globalData.opid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res)
        that.setData({
          list: res.data
        })
      }
    })
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=address_id', //默认地址ID
      data: {
        openid: app.globalData.opid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        that.setData({
          setAdId: res.data
        })
        wx.hideLoading()
      }
    })
  },
  setAd(e) {
    let that = this
    wx.showLoading({
      title: '处理中',
      mask: true
    })
    console.log(e.currentTarget.dataset.id, e.currentTarget.dataset.userid)
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=morendizhi',
      data: {
        address_id: e.currentTarget.dataset.id,
        user_id: e.currentTarget.dataset.userid,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res)
        wx.request({
          url: app.globalData.url + 'ziyu.php?Action=address_id', //默认地址ID
          data: {
            openid: app.globalData.opid
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            console.log(res)
            that.setData({
              setAdId: res.data
            })
            console.log(res)
            let pages = getCurrentPages()    //获取加载的页面
            let currentPage = pages[pages.length - 2];    //获取当前页面的对象
            // let url = currentPage.route;    //当前页面url
            wx.hideLoading();
            console.log(currentPage,pages);
            if (currentPage.route == 'pages/submit_order/submit_order' ) {
              wx.navigateTo({
                url: './../submit_order/submit_order'
              })
            }
           
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAddress()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})